<style>
.{{$t|escape:'html'}}_main {
  border-left: 15px solid #eaf2f5;
  border-right: 15px solid #eaf2f5;
  width: 1250px;
}
.{{$t|escape:'html'}}_subtitle {
  background-color: #e5f1f5;
  padding: 7px 0 7px 10px;
  font-family: Arial Black;
}
.{{$t|escape:'html'}}_emphasis {
  background-color: #d6e3e8;
  font-weight: bold;
  font-family: Trebuchet MS;
}
.{{$t|escape:'html'}}_eg {
  font-family: Trebuchet MS;
}
table.{{$t|escape:'html'}}_table td {
}
table.{{$t|escape:'html'}}_table td.title {
  font-weight: bold;
  text-align: right;
  padding: 2px 10px 2px 20px;
  margin-right: 20px;
  background-color: #d6e3e8;
  font-family: Trebuchet MS;
  width: 150px;
  height: 30px;
}
.{{$t|escape:'html'}}_select {
  width: 195px;
  height: 26px;
  margin-left: 4px; 
}
.{{$t|escape:'html'}}_text {
  width: 275px;
  height: 20px;
  margin-left: 4px;
}
.{{$t|escape:'html'}}_button {
  margin-top: 10px; 
  margin-left: 3px; 
}
.{{$t|escape:'html'}}_textarea {
  width: 275px;
  height: 80px;
  margin-left: 4px;
}
.{{$t|escape:'html'}}_status_text {
  font-weight: bold;
  padding-bottom: 5px;
  font-family: Arial;
}
.{{$t|escape:'html'}}_image_button {
  cursor: pointer;
}
table.{{$t|escape:'html'}}_listde {
  width: 100%;
  border-left: 1px solid #c8cddb;
  border-top: 1px solid #c8cddb;
}
table.{{$t|escape:'html'}}_listde td {
  padding-left: 10px;
  height: 35px;
  border-right: 1px solid #c8cddb;
  border-bottom: 1px solid #c8cddb;
  background-color: c8cddb;
  color: #ffffff;
  font-weight: bold;
}
table.{{$t|escape:'html'}}_listdrop {
  width: 100%;
  border: 0;
}
table.{{$t|escape:'html'}}_listdrop td {
  border: 0;
}
table.{{$t|escape:'html'}}_listdrop td.title {
  padding: 5px 10px 5px 20px;
  height: 35px;
  background-color: #dde1eb;
  font-weight: bold;
  text-align: right;
  width: 100px;
  color: #7b7b7b;
  font-size: 11px;
}
table.{{$t|escape:'html'}}_list {
  width: 100%;
  border-left: 1px solid #dddddd;
  border-top: 1px solid #dddddd;
  margin-bottom: 1px;
}
table.{{$t|escape:'html'}}_list th {
  text-align: left;
  height: 30px;
  background-color: #99b0ea;
  padding: 5px 0 5px 10px; 
  border-right: 1px solid #ffffff;
  border-bottom: 1px solid #ffffff;
  font-size: 12px;
}
table.{{$t|escape:'html'}}_list tr.drop td {
  padding-left: 0;
  border-right: 0;
  border-bottom: 0;
}
table.{{$t|escape:'html'}}_list tr.hi td {
  padding-left: 10px;
  height: 35px;
  background-color: #ff8000;
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
}
table.{{$t|escape:'html'}}_list tr.rowodd td {
  padding-left: 10px;
  height: 35px;
  background-color: #eeeeee;
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
}
table.{{$t|escape:'html'}}_list tr.roweven td {
  padding-left: 10px;
  height: 35px;
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
}
table.{{$t|escape:'html'}}_list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.{{$t|escape:'html'}}_list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.{{$t|escape:'html'}}_option td {
  background-color: #dde1eb;
}
table.{{$t|escape:'html'}}_optionde td.title {
  padding: 0 5px 0 0;
  font-weight: bold;
  text-align: right;
  color: #7b7b7b;
  font-size: 11px;
  height: 25px;
}
table.{{$t|escape:'html'}}_optionde input {
  width: 100px;
}
.{{$t|escape:'html'}}_scroll {
  overflow: auto;
  background-color: #ffffff;
}
.{{$t|escape:'html'}}_tip {
  padding-left: 10px;
  height: 35px;
  border-right: 1px solid #dddddd;
  border-left: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
  vertical-align: middle;
  line-height: 35px;
}
.{{$t|escape:'html'}}_tip:hover {
  background-color: #f4f3b2;
}
.{{$t|escape:'html'}}_select_day {
  width: 80px;
  height: 25px;
  margin-left: 4px;
}
.{{$t|escape:'html'}}_select_month {
  width: 80px;
  height: 25px;
  margin-left: 4px;
}
.{{$t|escape:'html'}}_select_year {
  width: 110px;
  height: 25px;
  margin-left: 4px;
}
</style>

<div class="{{$t|escape:'html'}}_main">
  <div>
    <div class="{{$t|escape:'html'}}_subtitle" style="text-align: center;background-color: #eaf2f5;">
      <span class="image_button" style="width: 100px;" onclick="save_transaction();">
        <table>
          <tr>
            <td><img src="image/btn/save24.png"></img></td>
            <td>{{t escape='html'}}Save{{/t}}</td>
          </tr>
        </table>
      </span>
      <span class="image_button" style="width: 100px;" onclick="history.back();">
        <table>
          <tr>
            <td><img src="image/btn/cancel24.png"></img></td>
            <td>{{t escape='html'}}Cancel{{/t}}</td>
          </tr>
        </table>
      </span>
    </div>
  <div>
  <div>
    <div class="{{$t|escape:'html'}}_subtitle">{{t escape='html'}}Basic Information{{/t}}</div>
    <table style="width: 100%;">
      <tr>
        <td style="width: 50%;vertical-align: top;">
          <table class="{{$t|escape:'html'}}_table">
            <tr>
              <td class="title">{{t escape='html'}}Transaction No.{{/t}}</td>
              <td><input id="{{$t|escape:'html'}}_transactionno" type="text" class="{{$t|escape:'html'}}_text" value=""></input><span class="{{$t|escape:'html'}}_eg"> {{t escape='html'}}(e.g. 51S614613P367010E){{/t}}</span></td>
            </tr>
            <tr>
              <td class="title">{{t escape='html'}}Total{{/t}}</td>
              <td><input id="{{$t|escape:'html'}}_total" type="text" class="{{$t|escape:'html'}}_text" value=""></input><span class="{{$t|escape:'html'}}_eg"> {{t escape='html'}}(e.g. $99.99){{/t}}</span></td>
            </tr>
            <tr>
              <td class="title">{{t escape='html'}}Paid Date{{/t}}</td>
              <td>
                <table cellpadding=0 cellspacing=0>
                  <tr>
                    <td>
                      <select id="{{$t|escape:'html'}}_paid_day" class="{{$t|escape:'html'}}_select_day"><option>{{t escape='html'}} - {{/t}}</option></select>
                    </td>
                    <td>
                      <select id="{{$t|escape:'html'}}_paid_month" class="{{$t|escape:'html'}}_select_month"><option>{{t escape='html'}} - {{/t}}</option></select>
                    </td>
                    <td>
                      <select id="{{$t|escape:'html'}}_paid_year" class="{{$t|escape:'html'}}_select_year"><option>{{t escape='html'}} - {{/t}}</option></select>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
            <tr>
              <td class="title">{{t escape='html'}}Comment{{/t}}</td>
              <td><textarea id="{{$t|escape:'html'}}_comment" type="text" class="{{$t|escape:'html'}}_textarea"></textarea></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </div>
</div>
<div>
<div class="{{$t|escape:'html'}}_subtitle">{{t escape='html'}}Order{{/t}}<span class="{{$t|escape:'html'}}_eg"> {{t escape='html'}}(One Transaction Per Order Applied) {{/t}}</span></div>
  <table class="{{$t|escape:'html'}}_table">
    <tr>
      <td class="title">{{t escape='html'}}Order No.{{/t}}</td>
      <td class="content" colSpan="3" style="width: 900px;"><input id="{{$t|escape:'html'}}_orderno" onchange="findOrder();" onkeyup="showTips();" type="text" class="{{$t|escape:'html'}}_text" style="width: 275px;" value=""></input><span class="{{$t|escape:'html'}}_eg"> {{t escape='html'}}(e.g. 20110822120430){{/t}}</span></td>
    </tr>
    <tr>
      <td style="width: 100px;"></td>
      <td class="content" colSpan="3" style="width: 900px; padding: 3px 0 3px 4px;" id="{{$t|escape:'html'}}_orderinfo"></td>
    </tr>
  </table>
</div>
<div>
  <div class="{{$t|escape:'html'}}_subtitle" style="text-align: center;background-color: #eaf2f5;">
    <span class="image_button" style="width: 100px;" onclick="save_transaction();">
      <table>
        <tr>
          <td><img src="image/btn/save24.png"></img></td>
          <td>{{t escape='html'}}Save{{/t}}</td>
        </tr>
      </table>
    </span>
    <span class="image_button" style="width: 100px;" onclick="history.back();">
      <table>
        <tr>
          <td><img src="image/btn/cancel24.png"></img></td>
          <td>{{t escape='html'}}Cancel{{/t}}</td>
        </tr>
      </table>
    </span>
  </div>
</div>

<script src="jsrequest.php?script={{$t|escape:'html'}}" type="text/javascript"></script>
<script type="text/javascript">
  <!--
    if (document.addEventListener) {
      document.addEventListener('DOMContentLoaded', init, false);
    }
    window.onload = init;
    var paid_day = document.getElementById('{{$t|escape:html}}_paid_day');
    var paid_month = document.getElementById('{{$t|escape:html}}_paid_month');
    var paid_year = document.getElementById('{{$t|escape:html}}_paid_year');
    
    var transNo = document.getElementById('{{$t|escape:html}}_transactionno');
    var total = document.getElementById('{{$t|escape:html}}_total');
    var comment = document.getElementById('{{$t|escape:html}}_comment');
    var orderNo = document.getElementById('{{$t|escape:html}}_orderno');
    
    var _transactions = {{if isset($transactions)}}JSON.parse('{{$transactions|@json_encode|escape:'javascript'}}'){{else}}[]{{/if}};
    var _orders = JSON.parse('{{$orders|@json_encode|escape:'javascript'}}');
    var _days = [];
    var _months = [];
    var _years = [];
    var _p = null;
    var _mark = 0;
    
    for (var i = 1, il = 31; i <= il; i++) {
      _days[_days.length] = (i < 10) ? '0' + i : i;
    }
    for (var j = 1, jl = 12; j <= jl; j++) {
      _months[_months.length] = (j < 10) ? '0' + j : j;
    }
    for (var k = 2011, kl = 2055; k <= kl; k++) {
      _years[_years.length] = k;
    }
    
    function init() {
      if (!document.getElementById || !document.createElement || !arguments || arguments.callee._loaded) {
        return;
      }
      arguments.callee._loaded = true;
      
      if (_orders.length > 0) {
        orderNo.value = _orders[0].order_no;
      }
      
      if (_transactions.length > 0) {
        transNo.value = _transactions[0].trans_no;
        total.value = _transactions[0].total;
        comment.innerHTML = _transactions[0].trans_ref;
      }
      setDate();
      setOrder();
    }
    
    function setDate() {
      var op1, op2, op3;
      for (var idx1 in _days) {
        var obj = _days[idx1];
        
        op1 = new Option(obj);
        op1._obj = obj;
        if (_transactions.length > 0) {
          if (op1.text == _transactions[0].date_paid.substr(8, 2)) {
            op1.selected = 'selected';
          }
        }
        paid_day.options[paid_day.options.length] = op1;
      }
      for (var idx2 in _months) {
        var obj = _months[idx2];
        
        op2 = new Option(obj);
        op2._obj = obj;
        if (_transactions.length > 0) {
          if (op2.text == _transactions[0].date_paid.substr(5, 2)) {
            op2.selected = 'selected';
          }
        }
        paid_month.options[paid_month.options.length] = op2;
      }
      for (var idx3 in _years) {
        var obj = _years[idx3];
        
        op3 = new Option(obj);
        op3._obj = obj;
        if (_transactions.length > 0) {
          if (op3.text == _transactions[0].date_paid.substr(0, 4)) {
            op3.selected = 'selected';
          }
        }
        paid_year.options[paid_year.options.length] = op3;
      }
    }
    
    function save_transaction() {
      var err = '';
      
      pay_day = (paid_day.options[paid_day.selectedIndex]._obj) ? paid_day.options[paid_day.selectedIndex]._obj : '-';
      pay_month = (paid_month.options[paid_month.selectedIndex]._obj) ? paid_month.options[paid_month.selectedIndex]._obj : '-';
      pay_year = (paid_year.options[paid_year.selectedIndex]._obj) ? paid_year.options[paid_year.selectedIndex]._obj : '-';
      
      if (_orders.length == 0) {
        err += '{{t escape='javascript'}}Must Enter a Valid Order No.{{/t}}' + '\n\n';
      }
      if (pay_day == '-') {
        err += '{{t escape='javascript'}}Must Select One Day.{{/t}}' + '\n\n';
      }
      if (pay_month == '-') {
        err += '{{t escape='javascript'}}Must Select One Month.{{/t}}' + '\n\n';
      }
      if (pay_year == '-') {
        err += '{{t escape='javascript'}}Must Select One Year.{{/t}}' + '\n\n';
      }
      
      if (err != '') {
        window.alert(err);
        return;
      }
      
      var transaction;
      
      transaction = _transactions[0];
      if (!transaction) {
        transaction = pz_ecom_transaction.instance();
        transaction.is_custom = 1;
      }
      
      transaction.trans_no = transNo.value;
      transaction.total = total.value;
      transaction.order_no = orderNo.value;
      transaction.trans_ref = comment.value;
      
      transaction._paidDay = pay_day;
      transaction._paidMonth = pay_month;
      transaction._paidYear = pay_year;
      
      transaction.customer_id = _orders[0].customer_id;
      transaction.payment_id = _orders[0].payment_id;
      transaction.payment_name = _orders[0].payment_name;
      transaction._oid = _orders[0].id;

      new RequestUtils()._custom('save_transaction', JSON.stringify(transaction), function(result, params) {
        location.href = 'admin.php?path=ecommerce&t=transactions';
      }, {
        php: 'ecommerce/custom.php',
        pos : DOMUtils.findPos(this)
      });
    }
    
    function setOrder() {
      var div = document.getElementById('{{$t|escape:'javascript'}}_orderinfo');
      DOMUtils.removeChildElements(div);
      if (_orders.length == 0 || (_orders.length > 0 && (_orders[0].is_paid == 1 && (_transactions.length > 0 && _transactions[0].id != _orders[0].transaction_id) || _transactions.length == 0))) {
        if (_orders.length > 0 && (_orders[0].is_paid == 1 && ((_transactions.length > 0 && _transactions[0].id != _orders[0].transaction_id) || _transactions.length == 0))) {
          window.alert('{{t escape='javascript'}}This Order Has Been Paid{{/t}}');
          _orders = [];
        }
        if (_transactions.length > 0 && _orders.length == 0) {
          div.style.color = '#ff0000';
          div.appendChild(document.createTextNode('{{t escape='javascript'}}Can Not Find The Old Order, Must Enter a New Valid Order No.{{/t}}'));
        } else if (_transactions.length == 0 && _orders.length == 0) {
          div.style.color = '#ff0000';
          div.appendChild(document.createTextNode('{{t escape='javascript'}}Must Enter a Valid Order No.{{/t}}'));
        }
        return;
      }
    }
    
    function findOrder() {
      var txt = document.getElementById('{{$t|escape:'javascript'}}_orderno');
      
      new RequestUtils()._custom('find_order', txt.value, function(result, params) {
        if (result.order == undefined) {
          _orders = [];
        } else {
          _orders = [ result.order ];
        }
        setOrder();
      }, {
        php: 'ecommerce/custom.php',
        pos : DOMUtils.findPos(this)
      });
    }

    function showTips() {
      _mark = MiscUtils.unique();
      setTimeout('showOrders(\'' + _mark + '\');', 250);
    }
    
    function showOrders(mark) {
      if (_mark != mark) {
        return;
      }
      
      var pos, div, table, tr, td, elm, txt;
      txt = document.getElementById('{{$t|escape:'javascript'}}_orderno');
      pos = DOMUtils.findPos(txt);
      
      div = document.createElement('div');
      div.style.backgroundColor = '#ffffff';
      div.style.borderBottom = '1px solid #dddddd';
      div.style.width = txt.offsetWidth + 'px';
      div.style.maxHeight = '200px';
      div.className = '{{$t|escape:'javascript'}}_scroll';
      table = document.createElement('table');
      tr = table.insertRow(-1);
      td = tr.insertCell(-1);
      elm = document.createElement('img');
      elm.src = 'image/loading.gif';
      td.appendChild(elm);
      td = tr.insertCell(-1);
      td.appendChild(document.createTextNode('{{t escape='javascript'}}Loading...{{/t}}'));
      div.appendChild(table);
      
      if (_p) {
        _p._close();
        _p = null;
      }
      _p = new Popup2(div, {
        pos: [ pos[0], pos[1] + txt.offsetHeight ]
      });
      
      document.body.onclick = function() {
        if (_p) {
          _p._close();
          _p = null;
        }
      };
      
      var cond_vals = {
        c: '`t`.`order_no` LIKE :v1',
        v: { v1: ('%' + txt.value + '%') }
      };
      if (txt.value.length > 0) {
//        new RequestUtils()._read('pz_ecom_order', cond_vals, null, null, null, null, function(result, params) {
        new RequestUtils()._custom('ezreadobj', JSON.stringify({tbl: 'pz_ecom_order', c: (JSON.stringify(cond_vals))}), function(result, params) {
          DOMUtils.removeChildElements(div);
          if (result.d.length > 0) {
            for (var idx in result.d) {
              var obj = result.d[idx];
              elm = document.createElement('div');
              elm.className = '{{$t|escape:'javascript'}}_tip';
              
              var span = getSpan(obj.order_no, txt.value);
              if (obj.is_paid == 1 && ((_transactions.length > 0 && _transactions[0].id != obj.transaction_id) || _transactions.length == 0)) {
                span.appendChild(document.createTextNode('{{t escape='javascript'}} (Paid){{/t}}'));
              }
              elm.appendChild(span);
              elm._obj = obj;
              elm.onclick = function () {
                txt.value = this._obj.order_no;
                findOrder();
              }
              div.appendChild(elm);
            }
          } else {
            elm = document.createElement('div');
            elm.className = '{{$t|escape:'javascript'}}_tip'
            elm.appendChild(document.createTextNode('{{t escape='javascript'}}No Orders Found{{/t}}'));
            div.appendChild(elm);
          }
        }, {
          php: 'custom.php'
        });
      } else {
        DOMUtils.removeTableRows(table, 0);
      }
    }
    
    function getSpan(result, search) {
      var pos = result.indexOf(search.toString());
      
      var str1, str2, str3;
      str1 = result.substr(0, pos);
      str2 = result.substr(pos, search.length);
      str3 = result.substr(pos + search.length);
      
      var span, sp1, sp2, sp3;
      sp1 = document.createElement('span');
      sp1.appendChild(document.createTextNode(str1));
      sp2 = document.createElement('span');
      sp2.style.backgroundColor = '#ff8000';
      sp2.appendChild(document.createTextNode(str2));
      sp3 = document.createElement('span');
      sp3.appendChild(document.createTextNode(str3));
      
      span = document.createElement('span');
      span.appendChild(sp1);
      span.appendChild(sp2);
      span.appendChild(sp3);
      return span;
    }
  // -->
</script>